package 代码随想2._5双指针法;

/**
 * @author XXX
 * @date 2024-01-17 15:47
 */

import 代码随想2._2链表.ListNode;

/**
 * https://leetcode.cn/problems/linked-list-cycle-ii/description/
 */
public class _8环形链表II {
    public ListNode detectCycle(ListNode head) {
        if (head == null)
            return head;
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow)
                break;
        }
        if (fast==null || fast.next==null)
            return null;
        fast = head;
        while (fast!=slow){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
